<?php

namespace App\Console\Commands;

use App\Model\Device;
use App\Model\InverterData;
use App\Model\InverterDataThree;
use App\Model\InverterDataTwo;
use App\Model\RecordDevicesDayGenCap;
use App\Model\Site;
use App\Model\StatisticsDeviceYearEnergy;
use App\Model\StatisticsDeviceYearEnergyCopy;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class RecordForExcel extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'RecordForExcel';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '统计每台逆变器一天的发电量';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */

    public function handle()
    {
        $begin = date('Y-m-d').' 23:59:59';
//        $begin = "2020-12-31 19:59:59";
        $table = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight',
            'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty', 'twentyone', 'twentytwo', 'twentythree', 'twentyfour', 'twentyfive', 'twentysix', 'twentyseven', 'twentyeight', 'twentynine', 'thirty', 'thirtyone'];

        $month = date('m',strtotime($begin));
        $year = date('Y',strtotime($begin));
//        $sites =  Site::whereIn('status',[1,2])->get();
        $sites = [1, 3, 5, 7, 9, 10, 11, 14, 15, 16, 17, 18, 19, 20, 21, 22, 25, 27, 28, 29, 31, 33,34,35, 37, 38,41, 43, 44, 45,46, 47, 48, 50, 51, 52, 54, 55,57, 58, 59,60,61];
//        $sites = [60,61];
//        $sites = [34,41,46,55];
        $filed = intval(date('d', strtotime($begin))) - 1;
//        $filed = 15;
        $index = $table[$filed];
        $results = [];
        foreach ($sites as $site) {
//            $id = $site->id;
            $id = $site;
            $devices = Device::where('site_id', $id)->get(['device_id']);
            switch ($id) {
                case $id < 18:
                    foreach ($devices as $device) {
                        $device_id = $device->device_id;
                        $obj = InverterData::where('site_id', $id)->where('device_id', $device_id)->where('DayGenCap', '<>', 0)->where('EventTime', '<', $begin)->orderBy('id', 'DESC')->first();
//                        $obj = InverterData::where('site_id', $id)->where('device_id', $device_id)->where('DayGenCap', '<>', 0)->where('created_at', '<', $begin)->orderBy('id', 'DESC')->first();
                        if ($obj) {
                            if (date('Y-m-d', strtotime($obj->EventTime)) == date('Y-m-d')) {
                                $device_info = ['site_id' => $id, 'device_id' => $device_id, $index => $obj->DayGenCap];
                            } else {
                                $device_info = ['site_id' =>$id, 'device_id' => $device_id, $index => 0];
                            }
//                            $device_info = ['site_id' => $id, 'device_id' => $device_id, $index => $obj->DayGenCap];
                        } else {
                            $device_info = ['site_id' =>$id, 'device_id' => $device_id, $index => 0];
                        }
//                        $device_info = ['site_id' => $site->id, 'device_id' => $device_id, $index => $obj->DayGenCap];
                        array_push($results, $device_info);
                    }
                    break;
                case $id >= 18 && $id < 43:
                    foreach ($devices as $device) {
                        $device_id = $device->device_id;
                        $obj = InverterDataTwo::where('site_id', $id)->where('device_id', $device_id)->where('DayGenCap', '<>', 0)->where('EventTime', '<', $begin)->orderBy('id', 'DESC')->first();
//                        $obj = InverterDataTwo::where('site_id', $id)->where('device_id', $device_id)->where('DayGenCap', '<>', 0)->where('created_at', '<', $begin)->orderBy('id', 'DESC')->first();
                        if ($obj) {
                            if (date('Y-m-d', strtotime($obj->EventTime)) == date('Y-m-d')) {
                                $device_info = ['site_id' => $id, 'device_id' => $device_id, $index => $obj->DayGenCap];
                            } else {
                                $device_info = ['site_id' => $id, 'device_id' => $device_id, $index => 0];
                            }
//                            $device_info = ['site_id' => $id, 'device_id' => $device_id, $index => $obj->DayGenCap];

                        } else {
                            $device_info = ['site_id' => $id, 'device_id' => $device_id, $index => 0];
                        }
//                        $device_info = ['site_id' => $site->id, 'device_id' => $device_id, $index => $obj->DayGenCap];
                        array_push($results, $device_info);
                    }
                    break;
                default:
                    foreach ($devices as $device) {
                        $device_id = $device->device_id;
                        $obj = InverterDataThree::where('site_id', $id)->where('device_id', $device_id)->where('DayGenCap', '<>', 0)->where('EventTime', '<', $begin)->orderBy('id', 'DESC')->first();
//                        $obj = InverterDataThree::where('site_id', $id)->where('device_id', $device_id)->where('DayGenCap', '<>', 0)->where('created_at', '<', $begin)->orderBy('id', 'DESC')->first();

                        if ($obj) {
                            if (date('Y-m-d', strtotime($obj->EventTime)) == date('Y-m-d')) {
                                $device_info = ['site_id' => $id, 'device_id' => $device_id, $index => $obj->DayGenCap];
                            } else {
                                $device_info = ['site_id' => $id, 'device_id' => $device_id, $index => 0];
                            }
//                            $device_info = ['site_id' => $id, 'device_id' => $device_id, $index => $obj->DayGenCap];
                        } else {
                            $device_info = ['site_id' => $id, 'device_id' => $device_id, $index => 0];
                        }
//                        $device_info = ['site_id' => $site->id, 'device_id' => $device_id, $index => $obj->DayGenCap];
                        array_push($results, $device_info);
                    }
            }
            DB::beginTransaction();
//            dump($results);
            foreach ($results as $result) {
                $param['site_id'] = $result['site_id'];
                $param['device_id'] = $result['device_id'];
                $param[$index] = $result[$index];
                $param['month'] = $month;
                $param['year'] = $year;

                $record = RecordDevicesDayGenCap::where('site_id', $result['site_id'])
                    ->where('device_id', $result['device_id'])
                    ->where('month', $month)
                    ->where('year', $year)
                    ->first();
                if ($record) {
//                    dump("up");
//                    dump($param);
                    $record->update($param);
                } else {
                    if ($result[$index] != 0) {
                        RecordDevicesDayGenCap::create($param);
//                        dump("up");
//                        dump($param);
                    }
                }
            }
            DB::commit();
        }
    }
}
